package org.sensornet2010.prototype;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Interface extends MIDlet implements CommandListener {

		
		private static int RECORDING_INTERVAL = 3600000;
		private static int RECORDING_DURATION = 3000;
		private static int SEND_DATA_INTERVAL;
		
		private Display display;
		private Command exitCommand;
		private Command startCommand;
		private Command OkCommand;
		private Command continueCommand;
		
		private StringItem debugInfo;
		private byte[] sound;
		
		
		public Interface(){
			
			this.display = Display.getDisplay(this);
			this.exitCommand = new Command("Exit", Command.EXIT, 1);
			this.startCommand = new Command("Start", Command.OK, 1);
			this.OkCommand = new Command("OK", Command.OK, 1);
			this.continueCommand = new Command("Continue", Command.OK, 1);
			
			this.debugInfo = new StringItem("DEBUGINFO", "debug...");
		
		}
		
		
		public void startApp(){
			
			Form welcome = new Form("SensorNetMobile");
			welcome.addCommand(exitCommand);
			welcome.addCommand(startCommand);
			welcome.append("Welcome! We're going to measure the CO2 concentrations using your mobile phone.\n" +
					"Please, press 'Start' if you want to start the application.");
			welcome.setCommandListener(this);
			display.setCurrent(welcome);
			
		}

		
		public void pauseApp(){
			
		}
		
		
		public void destroyApp(boolean unconditional){
			
		}
		
		
		public void commandAction(Command c, Displayable d) {
		
			if (c == this.exitCommand){
				destroyApp(false);
				notifyDestroyed();			
			} else if (c == this.startCommand){
				accessRights();			
			} else if (c == this.OkCommand){
				recording();
				recordSound();
			} else if (c == this.continueCommand){
				soundInfo();  
			}
		}
		
		
		public void accessRights(){
			
			Form access = new Form("ACCESS RIGHTS");
		
			access.addCommand(OkCommand);
			access.addCommand(exitCommand);
			access.append("We need to use the microphone of your mobile phone, are you agree on?");
			access.setCommandListener(this);
			display.setCurrent(access);
			
		}
		
		
		public void recording(){
			
			Form recording = new Form("RECORDING");
			
			recording.addCommand(continueCommand);
			recording.addCommand(exitCommand);
			recording.append("Recording... ... ... ...");
			recording.setCommandListener(this);
			display.setCurrent(recording);
			
		}
		
		public void soundInfo(){
			
			Form sound = new Form("SOUND INFO");
			
			sound.append("This is the information about the recorded sound: ....");
			sound.addCommand(exitCommand);
			sound.setCommandListener(this);
			display.setCurrent(sound);
			
		}
		
		public void debug(){
			
			Form deb = new Form("Debug Info");
			
			deb.append(debugInfo);
			display.setCurrent(deb);
		}
		
		
		////////////////////////////////////////////////////
		
		public void recordSound(){
			
			
		}
		
		public void analyseSound(){
			
			
		}
		
		public void sendSoundData(){
			
		}	
	
}
